<script type="text/ecmascript-6">
  import Navbar from '../../components/base/navbar.vue';
  import Verify from '../../components/base/verify-code.vue';

  import { Cache, cache_keys, env, is } from '../../utils/util';
  import { quickLogin,openidLogin, getOpenid, bindActivityOpenid,setUserCache } from '../../utils/oauth';
  export default{
      name:"user_login_quick",
      components:{
          Navbar,
          Verify
      },
      data(){
          return{
            code: '',
            mobile: '',
            sms_token: '',
            server: false,
          }
      },
      methods:{
        hint() {
          if (!this.server) {
            this.$Modal.info({title:"消息提示",body:'请仔细阅读用户协议后，勾选同意继续注册。'});
          }
        },
        login(){
            var message =null;
            if(!is.has(this.mobile)){
              message = '请输入您的手机号';
            } else if(!is.mobile(this.mobile)){
              message = '手机号格式不正确，请重新输入';
            } else if(!is.has(this.code)){
                message ='请输入验证码';
            }
            if(message){
                this.$Modal.info({title:"消息提示",body:message});
            }
            else{
                quickLogin.call(this);
            }
        }
      },
     beforeRouteEnter(to,from,next){
          if(env.isWechat){

              next(that=>{

                  var type=that.$route.query.type;
                  var openid =that.$route.query.openid || that.$route.query['?openid'];
                if (openid && type === 'oauth') {
//                    查询openid是否可以登录
                  openidLogin.call(that, {
                    onSuccess: null,
                    onError: function () {
//                      alert(1)
                      that.$Modal.info({title:"消息提示",body:'未能自动登录，请手动登录'});
                    }
                  });
                }else if(openid && type === 'activity'){
                  // 将openid写入缓存
                  Cache.set(cache_keys.activity_openid, { openid }, 0);
                  // 跳回之前的页面
                  var source = that.$route.query.source;
                  that.$router.replace(source);
                } else{
                  // 直接去取openid
                  getOpenid.call(that);
                }
                })
          }
          else{
             next();
          }
     }
  }
</script>
